package com.hmdp.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hmdp.dto.Result;
import com.hmdp.dto.UserDTO;
import com.hmdp.entity.Follow;
import com.hmdp.mapper.FollowMapper;
import com.hmdp.service.IFollowService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmdp.service.IUserService;
import com.hmdp.utils.UserHolder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 虎哥
 * @since 2021-12-22
 */
@Service
@Slf4j
public class FollowServiceImpl extends ServiceImpl<FollowMapper, Follow> implements IFollowService {
    @Resource
    private StringRedisTemplate stringRedisTemplate;
    @Resource
    private IUserService userService;

    @Override
    public Result follow(Long FollowId, boolean isFollow) {
        // 获取当前用户，形参id是被关注的用户
        Long userId = UserHolder.getUser().getId();
        String key = "follows" + userId;
        // 判断是取关还是关注
        if (isFollow) {
            // 关注,新增数据到数据库
            Follow follow = new Follow();
            follow.setUserId(userId);
            follow.setFollowUserId(FollowId);
            boolean flag = save(follow);
            // 如果数据库操作成功则将添加到set集合中
            if (flag) {
                stringRedisTemplate.opsForSet().add(key, FollowId.toString());
            }
        } else {
            // 取关,从数据库删除关联数据
            boolean flag = remove(new QueryWrapper<Follow>()
                    .eq("user_id", userId).eq("follow_user_id", FollowId));
            if (flag) {
                stringRedisTemplate.opsForSet().remove(key, FollowId.toString());
            }
        }
        return Result.ok();
    }

    @Override
    public Result isFollow(long id) {
        // 获取当前登录用户
        Long userId = UserHolder.getUser().getId();
        // 判断数据库中是否有值
        Integer count = query().eq("user_id", userId).eq("follow_user_id", id).count();
        return Result.ok(count > 0);
    }

    @Override
    public Result followCommon(Long id) {
        // 获取当前用户
        Long userId = UserHolder.getUser().getId();
        String key1 = "follows" + userId;
        String key2 = "follows" + id;
        // 求两者的交集
        Set<String> intersect = stringRedisTemplate.opsForSet().intersect(key1, key2);
        if (intersect == null || intersect.isEmpty()) {
            // 无交集
            return Result.ok(Collections.emptyList());
        }
        log.info("共同关注为:{}",intersect);
        // 此时一定有交集
        // 解析出交集id
        List<Long> ids = intersect.stream().map(Long::valueOf).collect(Collectors.toList());
        // 根据id查询并构建userDTO
        List<UserDTO> list = userService.listByIds(ids).stream()
                .map(user -> BeanUtil.copyProperties(user, UserDTO.class))
                .collect(Collectors.toList());
        return Result.ok(list);
    }


}
